package com.dhcc.DRGsHosp.application.impl;

import com.dhcc.DRGsHosp.application.MrBaseInfoApplication;
import com.dhcc.DRGsHosp.core.domain.MrBaseInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.transaction.annotation.Transactional;

import javax.inject.Named;
import java.util.List;
import java.util.Map;

/**
 * 首页表实体类 的   数据访问层--实体类
 * @ClassName: MrBaseInfoApplicationImpl  
 * @Description ：首页表实体类  数据访问层实体类
 * @Date: 2018年7月26日 下午9:17:24  
 * @Author QiaoYiLong 
 * @Version 3.1.1 DHC-DRG
 */

@Named("drgsHospMrBaseInfoApplicationImpl20181212")
@Transactional
public class MrBaseInfoApplicationImpl implements MrBaseInfoApplication {

	@Autowired
	private NamedParameterJdbcTemplate jdbcTempSeverice;

	/**  
	 *  功能描述：取得一个首页表实体类的信息
	 *  @Author QiaoYiLong
	 *  @param pmainIndex  主索引
	 *  @Date:2018年7月26日下午9:10:59
	 */
	@Override
	public MrBaseInfo getMrBaseInfo(String pmainIndex) {		
		return MrBaseInfo.get(MrBaseInfo.class , pmainIndex);
	}

	/**
	 * @param condition
	 * @return
	 * @throws
	 * @Description: 查询病人信息
	 * @author chenhanlin
	 * @date 2018/7/29 15:51
	 */
	@Override
	public List<MrBaseInfo> queryMrBaseInfoByCondition(Map<String, Object> condition) {
		StringBuffer sqlb = new StringBuffer() ;
		sqlb.append(" SELECT\n") ;
		sqlb.append(" a.PMAININDEX \"pmainIndex\",\n") ;
		sqlb.append(" P00010000    \"p00010000\",\n") ;
		sqlb.append(" P00010001    \"p00010001\",\n") ;
		sqlb.append(" P00020000    \"p00020000\",\n") ;
		sqlb.append(" P00020001    \"p00020001\",\n") ;
		sqlb.append(" P00030000    \"p00030000\",\n") ;
		sqlb.append(" P00040000    \"p00040000\",\n") ;
		sqlb.append(" P00050000    \"p00050000\",\n") ;
		sqlb.append(" P00060000    \"p00060000\",\n") ;
		sqlb.append(" P00070000    \"p00070000\",\n") ;
		sqlb.append(" P00070001    \"p00070001\",\n") ;
		sqlb.append(" P00080000    \"p00080000\",\n") ;
		sqlb.append(" P00090000    \"p00090000\",\n") ;
		sqlb.append(" P00090001    \"P00090001\",\n") ;
		sqlb.append(" P00090002    \"P00090002\",\n") ;
		sqlb.append(" P00100000    \"p00100000\",\n") ;
		sqlb.append(" P00110000    \"p00110000\",\n") ;
		sqlb.append(" P00120000    \"p00120000\",\n") ;
		sqlb.append(" P00130000    \"p00130000\",\n") ;
		sqlb.append(" P00130100    \"p00130100\",\n") ;
		sqlb.append(" P00130200    \"p00130200\",\n") ;
		sqlb.append(" P00130300    \"p00130300\",\n") ;
		sqlb.append(" P00140000    \"p00140000\",\n") ;
		sqlb.append(" P00140100    \"p00140100\",\n") ;
		sqlb.append(" P00140200    \"p00140200\",\n") ;
		sqlb.append(" P00150000    \"p00150000\",\n") ;
		sqlb.append(" P00160000    \"p00160000\",\n") ;
		sqlb.append(" P00170000    \"p00170000\",\n") ;
		sqlb.append(" P00180000    \"p00180000\",\n") ;
		sqlb.append(" P00190000    \"p00190000\",\n") ;
		sqlb.append(" P00190100    \"p00190100\",\n") ;
		sqlb.append(" P00190200    \"p00190200\",\n") ;
		sqlb.append(" P00190300    \"p00190300\",\n") ;
		sqlb.append(" P00190400    \"p00190400\",\n") ;
		sqlb.append(" P00190500    \"p00190500\",\n") ;
		sqlb.append(" P00190600    \"p00190600\",\n") ;
		sqlb.append(" P00200000    \"p00200000\",\n") ;
		sqlb.append(" P00200100    \"p00200100\",\n") ;
		sqlb.append(" P00200200    \"p00200200\",\n") ;
		sqlb.append(" P00200300    \"p00200300\",\n") ;
		sqlb.append(" P00200400    \"p00200400\",\n") ;
		sqlb.append(" P00200500    \"p00200500\",\n") ;
		sqlb.append(" P00210000    \"p00210000\",\n") ;
		sqlb.append(" P00210100    \"p00210100\",\n") ;
		sqlb.append(" P00210200    \"p00210200\",\n") ;
		sqlb.append(" P00220000    \"p00220000\",\n") ;
		sqlb.append(" P00220100    \"p00220100\",\n") ;
		sqlb.append(" P00220200    \"p00220200\",\n") ;
		sqlb.append(" P00220300    \"p00220300\",\n") ;
		sqlb.append(" P00220400    \"p00220400\",\n") ;
		sqlb.append(" CASE_FLAG    \"caseFlag\",\n") ;
		sqlb.append(" PUSH_DATE    \"pushDate\"\n") ;
		sqlb.append(" FROM \n") ;
		sqlb.append(" drgs.MR_BASEINFO a\n") ;
		sqlb.append(" LEFT JOIN drgs.MR_IPINFO ipinfo ON a.PMAININDEX = ipinfo.PMAININDEX\n") ;
		sqlb.append(" WHERE 1=1 \n ") ;
		String sql = sqlb.toString() ;

		// 时间条件
		if(condition.get("dateFrom")!=null && condition.get("dateTo") !=null){
			String dateField = "" ;
			// 时间字段
			if("outDate".equals(condition.get("dateType"))){ // 出院日期
				dateField = "ipinfo.P00290000" ;
			} else if("pushDate".equals(condition.get("dateType"))) { // 推送日期
				dateField = "a.push_date" ;
			}
			sql = sql.replace("1=1", " 1=1 and to_char("+dateField+",'yyyy-mm-dd')  BETWEEN :dateFrom AND :dateTo ") ;
		}

		if(condition.get("hospCode") != null){
			sql = sql.replace("1=1" ," 1=1 and instr(:hospCode,a.p00010001)>0 ") ;
		}

		// 分页sql
		String pageSqlHead = "SELECT * FROM (SELECT PAGERESULT.*, ROWNUM RN FROM (";
		String pageSqlFoot = " ORDER BY ipinfo.PMAININDEX desc) PAGERESULT ) WHERE rn>(:pageNo-1)*:pageSize and rn <=:pageNo*:pageSize ";
		List<MrBaseInfo> baseInfoList = jdbcTempSeverice.query(pageSqlHead+sql+pageSqlFoot,condition , new BeanPropertyRowMapper<MrBaseInfo>(MrBaseInfo.class));
		return baseInfoList;
	}

}
